package validators;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;


	@FacesValidator(value = "validators.email")
	public class UserEmailValidator implements Validator {
		private static final String PATTERN = "^[_A-Za-z0-9-]+(\\." +
				"[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" +
				"(\\.[A-Za-z]{2,})$";
		private Pattern pattern;
		private Matcher matcher;
		
		public UserEmailValidator() {
			pattern = Pattern.compile(PATTERN);
		}
		@Override
		public void validate(FacesContext context, UIComponent component,
				Object value) throws ValidatorException {
			matcher = pattern.matcher(value.toString());
			if(!matcher.matches()){
				FacesMessage msg =
						new FacesMessage("email validation failed.",
								"Email validation failed please enter a valid email");
								msg.setSeverity(FacesMessage.SEVERITY_ERROR);
						throw new ValidatorException(msg); }}}


